﻿using System;
using System.Data;
using System.Collections.Generic;
using Maticsoft.Common;
using Token.Piker.Model;
using Token.Piker.Common;
namespace Token.Piker.BLL
{
	/// <summary>
	/// PhotoBLL
	/// </summary>
	public partial class PhotoBLL
	{
		private readonly Token.Piker.DAL.PhotoDAL dal=new Token.Piker.DAL.PhotoDAL();
		public PhotoBLL()
		{}
		#region  BasicMethod

		/// <summary>
		/// 得到最大ID
		/// </summary>
		public int GetMaxId()
		{
			return dal.GetMaxId();
		}

		/// <summary>
		/// 是否存在该记录
		/// </summary>
		public bool Exists(int ID)
		{
			return dal.Exists(ID);
		}

		/// <summary>
		/// 增加一条数据
		/// </summary>
		public int  Add(Token.Piker.Model.Photo model)
		{
			return dal.Add(model);
		}

		/// <summary>
		/// 更新一条数据
		/// </summary>
		public bool Update(Token.Piker.Model.Photo model)
		{
			return dal.Update(model);
		}

		/// <summary>
		/// 删除一条数据
		/// </summary>
		public bool Delete(int ID)
		{
			
			return dal.Delete(ID);
		}
		/// <summary>
		/// 删除一条数据
		/// </summary>
		public bool DeleteList(string IDlist )
		{
			return dal.DeleteList(IDlist );
		}

		/// <summary>
		/// 得到一个对象实体
		/// </summary>
		public Token.Piker.Model.Photo GetModel(int ID)
		{
			
			return dal.GetModel(ID);
		}

		/// <summary>
		/// 得到一个对象实体，从缓存中
		/// </summary>
		public Token.Piker.Model.Photo GetModelByCache(int ID)
		{
			
			string CacheKey = "PhotoModel-" + ID;
			object objModel = Maticsoft.Common.DataCache.GetCache(CacheKey);
			if (objModel == null)
			{
				try
				{
					objModel = dal.GetModel(ID);
					if (objModel != null)
					{
						int ModelCache = Maticsoft.Common.ConfigHelper.GetConfigInt("ModelCache");
						Maticsoft.Common.DataCache.SetCache(CacheKey, objModel, DateTime.Now.AddMinutes(ModelCache), TimeSpan.Zero);
					}
				}
				catch{}
			}
			return (Token.Piker.Model.Photo)objModel;
		}

		/// <summary>
		/// 获得数据列表
		/// </summary>
		public DataSet GetList(string strWhere)
		{
			return dal.GetList(strWhere);
		}
		/// <summary>
		/// 获得前几行数据
		/// </summary>
		public DataSet GetList(int Top,string strWhere,string filedOrder)
		{
			return dal.GetList(Top,strWhere,filedOrder);
		}
		/// <summary>
		/// 获得数据列表
		/// </summary>
		public List<Token.Piker.Model.Photo> GetModelList(string strWhere)
		{
			DataSet ds = dal.GetList(strWhere);
			return DataTableToList(ds.Tables[0]);
		}
		/// <summary>
		/// 获得数据列表
		/// </summary>
		public List<Token.Piker.Model.Photo> DataTableToList(DataTable dt)
		{
			List<Token.Piker.Model.Photo> modelList = new List<Token.Piker.Model.Photo>();
			int rowsCount = dt.Rows.Count;
			if (rowsCount > 0)
			{
				Token.Piker.Model.Photo model;
				for (int n = 0; n < rowsCount; n++)
				{
					model = dal.DataRowToModel(dt.Rows[n]);
					if (model != null)
					{
						modelList.Add(model);
					}
				}
			}
			return modelList;
		}

		/// <summary>
		/// 获得数据列表
		/// </summary>
		public DataSet GetAllList()
		{
			return GetList("");
		}

		/// <summary>
		/// 分页获取数据列表
		/// </summary>
		public int GetRecordCount(string strWhere)
		{
			return dal.GetRecordCount(strWhere);
		}
		/// <summary>
		/// 分页获取数据列表
		/// </summary>
		public DataSet GetListByPage(string strWhere, string orderby, int startIndex, int endIndex)
		{
			return dal.GetListByPage( strWhere,  orderby,  startIndex,  endIndex);
		}
		/// <summary>
		/// 分页获取数据列表
		/// </summary>
		//public DataSet GetList(int PageSize,int PageIndex,string strWhere)
		//{
			//return dal.GetList(PageSize,PageIndex,strWhere);
		//}

		#endregion  BasicMethod

		#region  ExtensionMethod
        /// <summary>
        /// 获取首页最新上传的单张和主图的图片的列表
        /// </summary>
        /// <returns></returns>
        public DataSet GetHomePhotoList()
        {
            return dal.GetHomePhotoList();
        }
        /// <summary>
        /// 根据图片的ID取得上传者的用户名以及图片标签信息
        /// </summary>
        /// <param name="id">图片id</param>
        /// <returns></returns>
        public DataSet GetUserAndTagNameOfPhoto(int id)
        {
            return dal.GetUserAndTagNameOfPhoto(id);
        }

        /// <summary>
        /// 根据图片Id取得图片的有关信息
        /// 针对首页而写
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public DataSet GetPhotoInfoByID(int id)
        {
            return dal.GetPhotoInfoByID(id);
        }

        /// <summary>
        /// 根据图片的宽度和高度返回对应的缩略图的路径
        /// </summary>
        /// <param name="photo"></param>
        /// <param name="width"></param>
        /// <param name="height"></param>
        /// <returns></returns>
        public static string GetThumbnailSrc(Photo photo,int width,int height)
        {
            int index = FetchSize(width, height);
            string src = string.Empty;

            switch (index)
            {
                case 0:
                    src= photo.CompressUrl_1;
                    break;
                case 1:
                    src= photo.CompressUrl_2;
                    break;
                case 2:
                    src= photo.CompressUrl_3;
                    break;
                case 3:
                    src= photo.CompressUrl_4;
                    break;
            }

            return src;
        }

        /// <summary>
        /// 获取所对应的缩略图的索引
        /// </summary>
        /// <param name="width"></param>
        /// <param name="height"></param>
        /// <returns></returns>
        private static int FetchSize(int width, int height)
        {
            int fetch = 3;
            for (int i = 0; i < 4; i++)
            {
                if (width <= GlobalConstants.Photo_Size[1, i] && height <= GlobalConstants.Photo_Size[0, i])
                {
                    fetch = i;
                    break;
                }
            }
            return fetch;
        }
		#endregion  ExtensionMethod
	}
}

